package com.shqcjd.yunweiyanoperator.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.api.ApiService;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxObserver;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.yunweiyanoperator.model.ResponseData;
import com.shqcjd.yunweiyanoperator.model.pojo.RegisterCompanyBean;
import com.shqcjd.yunweiyanoperator.ui.activity.LicenseUpActivity;
import com.shqcjd.yunweiyanoperator.ui.activity.LoginActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BaseFragment;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;
import com.shqcjd.yunweiyanoperator.util.UIUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class CompanyRegisterFragment extends BaseFragment {

    @BindView(R.id.et_register_company_pwd)
    EditText etRegisterCompanyPwd;
    @BindView(R.id.tl_register_company_pwd)
    TextInputLayout tlRegisterCompanyPwd;
    @BindView(R.id.et_register_company_pwd_true)
    EditText etRegisterCompanyPwdTrue;
    @BindView(R.id.tv_register_company_next)
    TextView tvRegisterCompanyNext;
    @BindView(R.id.et_register_company)
    EditText etRegisterCompany;
    @BindView(R.id.et_register_company_business)
    EditText etRegisterCompanyBusiness;
    @BindView(R.id.et_register_company_name)
    EditText etRegisterCompanyName;
    @BindView(R.id.et_register_company_tel)
    EditText etRegisterCompanyTel;
    @BindView(R.id.tl_register_company)
    TextInputLayout tlRegisterCompany;
    @BindView(R.id.tl_register_company_business)
    TextInputLayout tlRegisterCompanyBusiness;
    @BindView(R.id.tl_register_company_name)
    TextInputLayout tlRegisterCompanyName;
    @BindView(R.id.tl_register_company_tel)
    TextInputLayout tlRegisterCompanyTel;


    private String mCode="-1";
    private Context mContext;

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_register_company;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    public static CompanyRegisterFragment newInstance() {
        return new CompanyRegisterFragment();
    }

    @Override
    public void init() {
        super.init();
        mContext = getActivity();
    }

    @Override
    public void initView(View rootView) {
        super.initView(rootView);

        etRegisterCompanyPwdTrue.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                password();
            }
        });

        addListener(tlRegisterCompany, etRegisterCompany, "企业名称不能为空");
        addListener(tlRegisterCompanyBusiness, etRegisterCompanyBusiness, "主营业务不能为空");
        addListener(tlRegisterCompanyName, etRegisterCompanyName, "管理员姓名不能为空");
        addListener(tlRegisterCompanyTel, etRegisterCompanyTel, "座机不能为空");

    }

    private boolean password() {
        if (etRegisterCompanyPwdTrue.getText().toString().length() >= etRegisterCompanyPwd.getText().toString().length() && !etRegisterCompanyPwdTrue.getText().toString().equals(etRegisterCompanyPwd.getText().toString())) {
            tlRegisterCompanyPwd.setError("密码不一致");
            return false;
        } else {
            tlRegisterCompanyPwd.setError(null);
            tlRegisterCompanyPwd.setErrorEnabled(false);

        }
        return true;
    }

    @OnClick({R.id.tv_register_company_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_register_company_next:
                if(!isNull()){
                        if(etRegisterCompanyPwd.getText().toString().equals(etRegisterCompanyPwdTrue.getText().toString())){
                            if(etRegisterCompanyPwdTrue.length()>=6){
                                    RegisterCompanyBean mBean = new RegisterCompanyBean();
                                    mBean.administratorName = etRegisterCompanyName.getText().toString().trim();
                                    mBean.businessLicenseUrl = "";
                                    mBean.businessName = etRegisterCompany.getText().toString().trim();
                                    mBean.mainBusiness = etRegisterCompanyBusiness.getText().toString().trim();
                                    mBean.password = etRegisterCompanyPwdTrue.getText().toString().trim();
                                    mBean.telNumber = etRegisterCompanyTel.getText().toString().trim();
                                    Intent it = new Intent(mContext, LicenseUpActivity.class);
                                    it.putExtra("mBean", mBean);
                                    startActivity(it);
                            }else{
                                ToastUtils.showShort(mContext, "密码必须大于等于6位");
                            }
                        }else{
                            ToastUtils.showShort(mContext, "密码不一致");
                        }
                }else{
                    ToastUtils.showShort(mContext, "请确认所有信息填写完毕");
                }
                break;
        }
    }

    private void getCodeHttp(String phone) {
        Map<String, String> map = new HashMap<>();
        map.put("phoneNumber", phone);
//        String encrypt = RSAUtils.encrypt(RSAUtils.PUCLIC_KEY, new JSONObject(map).toString());
        String json =  new JSONObject(map).toString();
        ApiService.getCode(json)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<String>>() {

                    @Override
                    public void _onNext(ResponseData<String> retData) {
                        if (retData != null && retData.retCode == 10000) {
                            mCode = retData.data;
                        } else {
                            ToastUtils.showShort(mContext, retData.retMsg);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        ToastUtils.showShort(mContext, errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }


    private boolean isNull(){
        if(!etRegisterCompanyPwd.getText().toString().equals("")&&
                !etRegisterCompanyPwdTrue.getText().toString().equals("")&&!etRegisterCompany.getText().toString().equals("")&&
                !etRegisterCompanyBusiness.getText().toString().equals("")&&!etRegisterCompanyName.getText().toString().equals("")&&
                !etRegisterCompanyTel.getText().toString().equals("")){
            return false;
        }
        return true;
    }

    private void addListener(TextInputLayout textInputLayout, EditText editText, String str){
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                notNull(textInputLayout, editText, str);
            }
        });
    }

    private boolean notNull(TextInputLayout textInputLayout, EditText editText, String str) {
        if (TextUtils.isEmpty(editText.getText().toString().trim())) {
            textInputLayout.setError(str);
            return false;
        } else {
            textInputLayout.setError(null);
            textInputLayout.setErrorEnabled(false);

        }
        return true;
    }
}
